package top.zsyle.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import top.zsyle.basic.util.AjaxResult;
import top.zsyle.system.domain.DictionaryDetail;
import top.zsyle.system.domain.DictionaryType;
import top.zsyle.system.mapper.DictionaryDetailMapper;
import top.zsyle.system.mapper.DictionaryTypeMapper;
import top.zsyle.system.service.DictionaryTypeService;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DictionaryTypeServiceImpl extends BaseServiceImpl<DictionaryType> implements DictionaryTypeService {

    @Autowired
    private DictionaryTypeMapper typeMapper;

    @Autowired
    private DictionaryDetailMapper detailMapper;

    @Override
    public void delete(Long id) {
        List<DictionaryDetail> details = detailMapper.findAllByTypeId(id);
        List<Long> detailIds = details.stream().map(DictionaryDetail::getId).collect(Collectors.toList());
        if (detailIds != null && detailIds.size()!=0){
            detailMapper.batchDelete(detailIds);
        }
        typeMapper.delete(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        List<DictionaryDetail> details = new ArrayList<>();
        for (Long id : ids) {
           details.addAll(detailMapper.findAllByTypeId(id));
        }
        //给list去重
        List<DictionaryDetail> newList = details.stream().distinct().collect(Collectors.toList());
        //把details集和中的id取出来放到id集和中
        List<Long> detailIds = newList.stream().map(DictionaryDetail::getId).collect(Collectors.toList());
        if (detailIds != null && detailIds.size()!=0){
            detailMapper.batchDelete(detailIds);
        }
        //批量删除类型
        typeMapper.batchDelete(ids);
    }
}
